package com.qipay.utils;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Creator: Yao
 * Date:    2017/10/9
 * For:
 * Other:
 */
@Component
public class NumGeneratorUtils {

    private final int randomNumberLength = 3;


    public String generate(Date date, int length) {
        return DateFormatUtils.format(date, "yyyyMMddHHmmssSSS") + RandomStringUtils.randomNumeric(length);
    }

    public String generate() {
        return generate(new Date(), randomNumberLength);
    }

    public String generate(Date date) {
        return generate(date, randomNumberLength);
    }

    public String generate(int length) {
        return generate(new Date(), length);
    }

    public static void main(String[] args) {
        NumGeneratorUtils utils = new NumGeneratorUtils();
        System.out.println(utils.generate());
        System.out.println(utils.generate(new Date()));
        System.out.println(utils.generate(3));
        String code= utils.generate(new Date(), 10);
        System.out.println(code);
        System.out.println(code.substring(0, 14));
    }
}
